package com.mhkj.execute.plan.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.common.util.ItemHelper;
import com.mhkj.execute.plan.entity.PlanNodeNew;
import com.mhkj.execute.plan.repository.PlanNodeNewRepository;
import com.mhkj.execute.plan.service.PlanNodeNewService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.util.UserHelper;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;

/**
 * 计划节点名称
 * 
 * @author mics
 * @date 2018年7月6日
 * @version 1.0
 */
@Service
public class PlanNodeNewServiceImpl extends BaseServiceImpl<PlanNodeNew> implements PlanNodeNewService {

	@Resource
	private PlanNodeNewRepository planNodeNewRepository;
	@Resource
	private OrgService orgService;
	@Resource
	private ItemHelper itemHelper;

	@Resource
	public void setPlanNodeNewRepository(PlanNodeNewRepository planNodeNewRepository) {
		super.setBaseRepository(planNodeNewRepository);
	}

	@Override
	public Pagination<PlanNodeNew> newNodelistByPage(PlanNodeNew planNode, String startDate, String endDate,
			Long itemId, int pageIndex, int pageSize) {
		Specification<PlanNodeNew> spec = new Specification<PlanNodeNew>() {
			private static final long serialVersionUID = -786003485941679959L;

			@Override
			public Predicate toPredicate(Root<PlanNodeNew> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Join<Object, Object> plan = root.join("plan", JoinType.INNER);
				if (!StringUtils.isEmpty(planNode.getSign())) {
					Predicate p1 = criteriaBuilder.equal(root.get("sign").as(String.class), planNode.getSign());
					lstPredicates.add(p1);
				}
				if (!StringUtils.isEmpty(planNode.getNodeName())) {
					Predicate p2 = criteriaBuilder.like(root.get("nodeName").as(String.class),
							"%" + planNode.getNodeName() + "%");
					lstPredicates.add(p2);
				}
				if (!StringUtils.isEmpty(startDate)) {
					try {
						Date endDate1 = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
						Date beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
						Predicate p4 = criteriaBuilder.lessThanOrEqualTo(root.get("beginDate"), endDate1);
						Predicate p5 = criteriaBuilder.greaterThanOrEqualTo(root.get("endDate"), beginDate);
						lstPredicates.add(p4);
						lstPredicates.add(p5);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				Join<Object, Object> item = plan.join("item",JoinType.LEFT);
                if(itemHelper.getCurrentUserItems().size() > 0 ){
					Predicate p3=criteriaBuilder.and(item.in(itemHelper.getCurrentUserItems()));
					Predicate p4=criteriaBuilder.and(item.isNull());
		            lstPredicates.add(criteriaBuilder.or(p3,p4));
                }else{
					Predicate p3=criteriaBuilder.and(item.isNull());
		            lstPredicates.add(p3);
                }
				if (!StringUtils.isEmpty(itemId)) {
					lstPredicates.add(criteriaBuilder.equal(item.get("id").as(Long.class), itemId));
				}	            
				lstPredicates.add(criteriaBuilder.equal(root.get("createBy").as(String.class), UserHelper.getCurrentUserId()));

				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		Page<PlanNodeNew> pageT = planNodeNewRepository.findAll(spec,
				PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id"));
		Pagination<PlanNodeNew> pagination = new Pagination<PlanNodeNew>();
		pagination.setTotal(planNodeNewRepository.count(spec));
		List<PlanNodeNew> plans = pageT.getContent();
		for (PlanNodeNew planNode2 : plans) {
			List<Employee> postInfo = orgService.getEmployeeByPost(planNode2.getPostId());
			if (postInfo.size() > 0) {
				planNode2.setPostName(postInfo.get(0).getName());
			}
			if (!planNode2.getPostPerson().isEmpty()) {
				planNode2.setPostPersonName(orgService.employeeInfo(planNode2.getPostPerson()).getName());
			}
		}
		pagination.setData(plans);
		return pagination;
	}

}
